package com.mt.api.modules.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mt.api.modules.domain.AppCustomer;
import com.mt.api.modules.domain.bo.CustomerListBo;
import com.mt.api.modules.domain.vo.CustomerWithAmountVo;
import com.mt.api.modules.manager.ICustomerManager;
import com.mt.api.core.page.PageFactory;
import com.mt.api.modules.domain.bo.CustomerPageBo;
import com.mt.api.modules.mapper.AppCustomerMapper;
import com.mt.api.util.AmountUtil;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author: plover
 * @Date: 2022-01-02
 * @Description:
 */
@Component
public class CustomerManagerImpl extends ServiceImpl<AppCustomerMapper, AppCustomer> implements ICustomerManager {


    @Override
    public Page getPageList(CustomerPageBo customerPageBo) {
        Page page = PageFactory.defaultPage(customerPageBo.getPageSize(), customerPageBo.getPageNum());
        Page customerPage = baseMapper.selectPage(page, Wrappers.<AppCustomer>lambdaQuery()
                .orderByDesc(AppCustomer::getId));
        return customerPage;
    }

    @Override
    public Page<CustomerWithAmountVo> getCustomerStatPage(CustomerPageBo customerPageBo) {
        Page page = PageFactory.defaultPage(customerPageBo);
        List<CustomerWithAmountVo> list = baseMapper.getCustomerStatPage(page, customerPageBo);
        for (CustomerWithAmountVo customerWithAmountVo : list) {
            customerWithAmountVo.setSalesAmount(AmountUtil.divideHundred(customerWithAmountVo.getSalesAmount()));
            customerWithAmountVo.setSettleAmount(AmountUtil.divideHundred(customerWithAmountVo.getSettleAmount()));
            customerWithAmountVo.setUnSettleAmount(AmountUtil.divideHundred(customerWithAmountVo.getUnSettleAmount()));
        }
        page.setRecords(list);
        return page;
    }

    @Override
    public List<AppCustomer> getListByParam(CustomerListBo bo) {
        LambdaQueryWrapper<AppCustomer> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(AppCustomer::getReceiver, bo.getReceiver())
                .eq(AppCustomer::getRphone, bo.getRphone());
        return baseMapper.selectList(wrapper);
    }

}
